package com.example.demo.controller;

import com.example.demo.entity.User;
import com.example.demo.request.AddRequest;
import com.example.demo.request.EditRequest;
import com.example.demo.response.AddResponse;
import com.example.demo.response.EditResponse;
import com.example.demo.service.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/test")
public class TestController {

    @Autowired
    private TestService testService;

    @PostMapping(value = "/add")
    public AddResponse add(@Validated @RequestBody AddRequest request) {
        return testService.add(request);
    }

    @PostMapping(value = "/edit")
    public EditResponse edit(@Validated @RequestBody EditRequest request) {
        return testService.edit(request);
    }

    @GetMapping("/users")
    public List<User> getUserList() {
        List<User> users = new ArrayList<>();
        users.add(new User());
        users.add(new User());
        return users;
    }

    @GetMapping("/getUserByName")
    public User getUserByName(String username) {
        User user = new User();
        user.setUsername(username);
        user.setPassword("123");
        return user;
    }

    @GetMapping("/getUser")
    public User getUser(User user) {
        user.setUsername(user.getUsername() + ",UK");
        user.setPassword(user.getPassword() + ",US");
        return user;
    }

    /**
     * {id:\d+} 正则表达式表明id只能是数字
     */
    @GetMapping("/user/{id:\\d+}")
    public User getUserInfo(@PathVariable("id") String id) {
        return new User();
    }

    @PostMapping("/createUser")
    public User createUser(@RequestBody User user) {
        user.setAge(30);
        return user;
    }
}
